﻿// Copyright 2013 Ronald Schlenker, www.technewlogic.de.
// 
// This file is part of Byond.
// 
// Byond is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
// 
// Byond is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with Byond.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Threading;

namespace Utils.DialogManagement
{
	public class WaitProgressDialog : DialogBase, IProgressDialog
	{
		private readonly UiDispatcher _dispatcher;

		#region Factory

		public static IWaitDialog CreateWaitDialog(
			DialogLayeringHelper dialogLayeringHelper,
			DialogMode dialogMode,
			UiDispatcher dispatcher)
		{
			IWaitDialog dialog = null;
			dispatcher.Invoke(() => dialog = new WaitProgressDialog(
				dialogLayeringHelper, dialogMode, true, dispatcher));
			return dialog;
		}

		public static IProgressDialog CreateProgressDialog(
			DialogLayeringHelper dialogLayeringHelper,
			DialogMode dialogMode,
			UiDispatcher dispatcher)
		{
			IProgressDialog dialog = null;
			dispatcher.Invoke(
				() => dialog = new WaitProgressDialog(
					dialogLayeringHelper, dialogMode, false, dispatcher));
			return dialog;
		}

		#endregion

		private WaitProgressDialog(
			DialogLayeringHelper dialogLayeringHelper,
			DialogMode dialogMode,
			bool showWaitAnimation,
			UiDispatcher dispatcher)
			: base(dialogLayeringHelper, dialogMode, dispatcher)
		{
			_dispatcher = dispatcher;
			_waitProgressDialogControl = new WaitProgressDialogControl(showWaitAnimation);
			SetContent(_waitProgressDialogControl);
		}

		private readonly WaitProgressDialogControl _waitProgressDialogControl;
		private bool _isReady;

		#region Implementation of IMessageDialog

		public string Message
		{
			get
			{
				var text = string.Empty;
				InvokeUICall(
					() => text = _waitProgressDialogControl.DisplayText);
				return text;
			}
			set
			{
				InvokeUICall(
					() => _waitProgressDialogControl.DisplayText = value);
			}
		}

		#endregion

		#region Implementation of IWaitDialog

		public Action WorkerReady { get; set; }
		public bool CloseWhenWorkerFinished { get; set; }

		private string _readyMessage;
		public string ReadyMessage
		{
			get { return _readyMessage; }
			set
			{
				_readyMessage = value;
				if (_isReady)
					InvokeUICall(
						() => _waitProgressDialogControl.DisplayText = value);
			}
		}

		private readonly ManualResetEvent _beginWork =
			new ManualResetEvent(false);

		public void Show(Action workerMethod)
		{
			ThreadPool.QueueUserWorkItem(o =>
			{
				try
				{
					_beginWork.WaitOne(-1);

					workerMethod();

					InvokeUICall(() =>
					{
						_isReady = true;

						if (WorkerReady != null)
							WorkerReady();

						if (CloseWhenWorkerFinished)
						{
							Close();
							return;
						}

						_waitProgressDialogControl.DisplayText = ReadyMessage;
						_waitProgressDialogControl.Finish();

						DialogBaseControl.RemoveButtons();
						DialogBaseControl.AddOkButton();
					});
				}
				catch (Exception ex)
				{
					InvokeUICall(() =>
					{
						Close();
						throw ex;
					});
				}
			});

			Show();

			_beginWork.Set();
		}

		public void InvokeUICall(Action uiWorker)
		{
		}

		#endregion

		#region Implementation of IProgressDialog

		public int Progress
		{
			get
			{
				var progress = 0;
				InvokeUICall(
					() => progress = _waitProgressDialogControl.Progress);
				return progress;
			}
			set
			{
				InvokeUICall(
					() => _waitProgressDialogControl.Progress = value);
			}
		}

		#endregion
	}
}